<?php

@include 'db.inc.php';
@include 'queries.inc.php';

error_reporting(E_ALL ^  E_NOTICE);

$Excel_Statement_Export = $_POST['Excel_Statement'];
$Excel_Variable_Export = $_POST['Excel_Variable'];
$länge = strlen($Excel_Variable_Export);

if($länge>0){
        $select = $Excel_Statement_Export."'".$Excel_Variable_Export."'";
}
    else{
        $select = $Excel_Statement_Export;
    }

mysql_query("SET NAMES 'latin_general_ci'");
$export = mysql_query($select);
$fields = mysql_num_fields($export);

for ($i = 0; $i < $fields; $i++) { 
    $header .= mysql_field_name($export, $i) . "\t"; 
} 

while($row = mysql_fetch_row($export)) { 
    $line = ''; 
    foreach($row as $value) {                                             
        if ((!isset($value)) OR ($value == "")) { 
            $value = "\t"; 
        } else { 
            $value = str_replace('"', '""', $value); 
            $value = '"' . $value . '"' . "\t"; 
        } 
        $line .= $value; 
    } 
    $data .= trim($line)."\n"; 
} 
$data = str_replace("\r","",$data);  

if ($data == "") { 
    $data = "\n(0) Records Found!\n";                         
} 

$excel_export_filename = "archiv_export_".date("Y-m-d_H-i",time());

header("Content-type: application/octet-stream"); 
header("Content-Disposition: attachment; filename=".$excel_export_filename.".xls"); 
header("Pragma: no-cache"); 
header("Expires: 0"); 
print "$header\n$data";  

?>
